#define _CRT_SECURE_NO_WARNINGS   1

#include<iostream>
#include<algorithm>
using namespace std;

typedef int Int;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 100005

int exgcd(int a, int b, int &x, int &y)
{
	if (b == 0)
	{
		x = 1;
		y = 0;
		return a;
	}
	int ans = exgcd(b, a%b, y, x);
	y -= (a / b)*x;
	return ans;
}

void solve(int a, int b, int c)
{
	int x, y;
	int gcd = exgcd(a, b, x, y);
	if (c%gcd != 0)
	{
		cout << -1 << endl;
		return;
	}
	x *= c / gcd;
	y *= c / gcd;
	a /= gcd;
	b /= gcd;
	int mid = (y - x) / (a + b);
	int ans = 1e08;
	for (int i = mid - 1; i <= mid + 1; i++)
	{
		int tmp = 0;
		if (abs(x + b*i) + abs(y - a*i) == abs(x + b*i + y - a*i))
			tmp = max(abs(x + b*i), abs(y - b*i));
		else
			tmp = abs(x + b*i) + abs(y - a*i);
		ans = min(ans, tmp);
	}
	cout << ans << endl;
}

Int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int A, B, a, b;
		cin >> A >> B >> a >> b;
		int c = abs(B-A);
		solve(a, b, c);
	}
	return 0;
}